package com.example.demo.controller;

import com.example.demo.common.JsonResult;
import com.example.demo.dao.WuZiDao;
import com.example.demo.entity.Ruku;
import com.example.demo.entity.Wuzi;
import com.example.demo.service.WuZiService;
import com.example.demo.vo.WuziVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("wuzi")
public class WuZiController {

    @Autowired
    WuZiService wuZiService;

    @ApiOperation(value = "查询所有物资信息")
    @PostMapping("findAll")
    public JsonResult<List<WuZiDao>> findAll(){
        List<WuZiDao> list = wuZiService.findAll();
        return JsonResult.ok(list);
    }

    @ApiOperation(value = "根据物资编号查询物资信息")
    @PostMapping("findByWzname")
    public JsonResult<List<WuziVO>> findByWzname(@ApiParam(name = "wzname",value = "物资名称",required = true)String wzname){
            List<WuziVO> list = wuZiService.findByWzname(wzname);
            return JsonResult.ok(list);
    }

    @ApiOperation(value = "插入物资信息")
    @PostMapping("insert")
    public JsonResult insert(@ApiParam(name = "wuzi",value = "物资",required = true)@RequestBody Wuzi wuzi){
        try {
            int r = wuZiService.insert(wuzi);
            if(r > 0){
                return JsonResult.ok();
            }else{
                return JsonResult.fail(5003,"物资添加失败！！");
            }
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.fail();
        }
    }

    @ApiOperation(value = "删除指定名称物资信息")
    @PostMapping("delete")
    public JsonResult delete(@ApiParam(name = "wzname",value = "物资名称",required = true)String wzname){
        try{
            int r = wuZiService.delete(wzname);
            if(r > 0){
                return JsonResult.ok();
            }else{
                return JsonResult.fail(5003,"物资删除失败！！");
            }
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.fail(5000, "服务器内部错误，更新操作失败：" + e.getMessage());
        }
    }

    @ApiOperation(value = "更新物质表")
    @PostMapping("update")
    public JsonResult update(@RequestBody Wuzi wuzi) {
        try {
            int r = wuZiService.update(wuzi); // 调用服务层的update方法
            if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5002, "物资更新失败！！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail(5000, "服务器内部错误，更新操作失败：" + e.getMessage());
        }
    }

    @ApiOperation(value = "更新入库信息")
    @PostMapping("rukuUpdate")
    public JsonResult rukuUpdate(@RequestBody Ruku ruku) {
        try {
            int r = wuZiService.rukuUpdate(ruku); // 调用服务层的update方法
            if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5003, "入库物资更新失败！！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail(5000, "服务器内部错误，更新操作失败：" + e.getMessage());
        }
    }

    @ApiOperation(value = "查询入库物资信息")
    @PostMapping("findRuku")
    public JsonResult<List<WuziVO>> findRuku(){
        List<WuziVO> list= wuZiService.findRuku();
        return JsonResult.ok(list);
    }

    @ApiOperation(value = "更新入库物资数量")
    @PostMapping("UprkNums")
    public JsonResult UprkNums(@RequestBody Wuzi wuzi){
        try{
            int r = wuZiService.UprkNums(wuzi);
            if(r > 0){
                return JsonResult.ok();
            }else{
                return JsonResult.fail(5005,"入库物资数量更新失败！！");
            }
        }catch (Exception e){
            e.printStackTrace(); //打印
            return JsonResult.fail(5000, "服务器内部错误，更新操作失败：" + e.getMessage());
        }
    }

    //更新出库物资数量
    @ApiOperation(value = "更新出库物资数量")
    @PostMapping("UpckNums")
    public JsonResult UpckNums(@RequestBody Wuzi wuzi){
        try{
            int r = wuZiService.UpckNums(wuzi);
            if(r > 0){
                return JsonResult.ok();
            }else{
                return JsonResult.fail(5005,"入库物资数量更新失败！！");
            }
        }catch (Exception e){
            e.printStackTrace(); //打印
            return JsonResult.fail(5000, "服务器内部错误，更新操作失败：" + e.getMessage());
        }
    }
}
